package com.meshnetics.gb.stack.mac.primitives;

import com.meshnetics.gb.stack.core.event.StackEventType;
import com.meshnetics.gb.stack.core.event.StackRequest;
import com.meshnetics.gb.stack.mac.RadioMACStackLayer;
import com.meshnetics.gb.type.impl.zcl.TypeUInt;
import com.meshnetics.gb.type.impl.zcl.UInt;

import java.nio.ByteBuffer;

/**
 * WRITE DESCRIPTION HERE
 *
 * @author AKovylin
 */
public class MACPurgeRequest extends StackRequest implements MACCommand, MACReplyAwaited {
    public static final byte CODE = 0x43;

    private final UInt msduHandle;

    public MACPurgeRequest(int msduHandle) {
        this((byte) (msduHandle & 0xff));
    }

    public MACPurgeRequest(byte msduHandle) {
        this(new Byte(msduHandle));
    }

    public MACPurgeRequest(Byte msduHandle) {
        this((UInt) TypeUInt.UINT_8.fromObject(msduHandle));
    }

    public MACPurgeRequest(UInt msduHandle) {
        super("macPurgeRequest", StackEventType.LDE_TYPE);
        if (msduHandle == null) {
            throw new NullPointerException("msduHandle is null");
        }
        this.msduHandle = msduHandle.convert(TypeUInt.UINT_8);
    }

    public UInt getMsduHandleType() {
        return msduHandle;
    }

    public Byte getMsduHandle() {
        return (Byte) msduHandle.toObject();
    }

    public byte getCode() {
        return CODE;
    }

    public int getSize(RadioMACStackLayer layer) {
        return 1;
    }

    public void serialize(ByteBuffer bb, RadioMACStackLayer layer) {
        msduHandle.serialize(bb);
    }

    public String toString() {
        return getName() + '(' + msduHandle + ')';
    }
}
